package com.shire.shire_interface.service.account.impl;

import com.shire.shire_interface.common.utils.JsonFormat;
import com.shire.shire_interface.common.utils.StrUtils;
import com.shire.shire_interface.constants.Constants;
import com.shire.shire_interface.constants.DeviceCode;
import com.shire.shire_interface.dao.accounts.WxStationDao;
import com.shire.shire_interface.model.account.DeviceInfo;
import com.shire.shire_interface.model.account.WxStation;
import com.shire.shire_interface.model.account.WxStationExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@CacheConfig(cacheNames = {"wx_station"})
@Service
public class WxStationService {

    @Autowired
    private WxStationDao wxStationDao;

    @Autowired
    private DeviceCodeService deviceCodeService;

    @Autowired
    private LocationService locationService;

    public int insertWxStation(WxStation wxStation){
        return wxStationDao.insert(wxStation);
    }

    public int updateWxStation(WxStation wxStation){
        return wxStationDao.updateByPrimaryKey(wxStation);
    }

    @Cacheable(key = "#p0 +#p1")
    public WxStation getStationByNK(String n, String k){
        WxStationExample stationExample = new WxStationExample();
        if (n!=null)
            stationExample.createCriteria().andRealudidEqualTo(k).andStationnoEqualTo(n);
        else
            stationExample.createCriteria().andRealudidEqualTo(k);
        List<WxStation> stations = wxStationDao.selectByExample(stationExample);
        if (stations.size()>0){
            return stations.get(0);
        }

        return null;
    }

    public WxStation genNewStation(DeviceInfo deviceInfo, String stationNo, String city){
        WxStation wxStation = new WxStation();
        wxStation.setRealudid(deviceInfo.getRealudid());
        wxStation.setStationno(stationNo);
        DeviceCode deviceCode = genDeviceCode(deviceInfo, city);
        wxStation.setParajson(JsonFormat.toJson(deviceCode));
        wxStation.setStatus(Constants.CommonStatus.ENABLE.ordinal());
        wxStation.setCreatetime(new Date());
        wxStation.setUpdatetime(wxStation.getCreatetime());
        insertWxStation(wxStation);
        return wxStation;
    }

    public DeviceCode genDeviceCode(DeviceInfo deviceInfo, String city){
        DeviceCode deviceCode = new DeviceCode();
        if(StrUtils.getNullStr(deviceInfo.getDevicemodel()).toLowerCase().indexOf("5s")>0){ // 5s手机
            deviceCode.dev_type_name = "iphone5s";
            deviceCodeService.genByRandom(deviceCode);
        }
        else {
            // 临时限制，如果传入的手机是plus，那么返回的模拟参数也需要是plus，normal->normal
            Boolean origPlus = isPlus(deviceInfo.getDevicemodel());
            for (int i = 0; i < 100; i++) {
                deviceCodeService.genByRandom(deviceCode);
                Boolean randomPlus = isPlus(deviceCode.dev_type_name);
                if (origPlus.equals(randomPlus)) {
                    break;
                }
                if (i == 99) {
                    return null;
                }
            }
        }
        deviceCode.location = locationService.getSpecCityLocation(city);
        return deviceCode;
    }

    public Boolean isPlus(String deviceName){
        if(StrUtils.isBlank(deviceName)) return false;
        if(deviceName.toLowerCase().indexOf("plus")>0
                || deviceName.toLowerCase().indexOf("x")>0){
            return true;
        }
        return false;
    }
}
